package com.yhb.service.impl;

import com.yhb.dao.AddressMapper;
import com.yhb.model.Address;
import com.yhb.service.AddressService;
import lombok.extern.log4j.Log4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void modify(Address address) {

        addressMapper.modifyAddress(address);

        if (((Integer)1).equals(address.getIsDefault())) { // 默认地址刷新
            var addresses = userAddress(address.getUserId());
            for (var add : addresses) {
                if (!add.getId().equals(address.getId())) {
                    add.setIsDefault(0);
                    addressMapper.modifyAddress(add);
                }
            }
        }
    }

    @Override
    public void add(Address address) {
        addressMapper.addAddress(address);
    }

    @Override
    public void delete(Address address) {
        addressMapper.deleteAddress(address);
    }

    @Override
    public List<Address> userAddress(String userId) {
        return addressMapper.userAddress(userId);
    }

    @Override
    public Address addressById(String addressId) {
        return addressMapper.addressById(addressId);
    }
}
